import {getBusinessObject, is} from 'bpmn-js/lib/util/ModelUtil';
import elementHelper from "bpmn-js-properties-panel/lib/helper/ElementHelper";
import cmdHelper from "bpmn-js-properties-panel/lib/helper/CmdHelper";


/**
 * 节点类型
 */
const NodeType = {

  FORM_TASK: 'FORM_TASK',
  EXAMINE_TASK: 'EXAMINE_TASK',
  SEND_TASK: 'SEND_TASK',
  CONDITION_NODE: 'CONDITION_NODE',
  SEQUENCE_FLOW: 'SEQUENCE_FLOW',
  START_NODE: 'START_NODE',
  END_NODE: 'END_NODE',
  ROOT: 'ROOT',

  getType(type, valueType) {
    if (type === 'bpmn:UserTask' && valueType === 'formTask') {
      return NodeType.FORM_TASK;
    }
    if (type === 'bpmn:UserTask' && valueType === 'examineTask') {
      return NodeType.EXAMINE_TASK;
    }
    if (type === 'bpmn:SendTask') {
      return NodeType.SEND_TASK;
    }
    if (type === 'bpmn:SequenceFlow') {
      return NodeType.SEQUENCE_FLOW;
    }
    if (type === 'bpmn:ExclusiveGateway') {
      return NodeType.CONDITION_NODE;
    }
    if (type === 'bpmn:StartEvent') {
      return NodeType.START_NODE;
    }
    if (type === 'bpmn:EndEvent') {
      return NodeType.END_NODE;
    }
    if (type === 'bpmn:Process') {
      return NodeType.ROOT;
    }
    console.error('未知类型' + type + ' ' + valueType);
  }
};

class TaskNode {

  constructor(bo) {
    this._bo = bo;
    this._props = {};
    this.updateType();
  }

  updateType() {
    this._updateProperty();
    this.type = NodeType.getType(this._bo.$type, this._props.type);
  }

  isProcess() {
    return this._eType === 'bpmn:Process';
  }

  isUserTask() {
    return this._eType === 'bpmn:UserTask';
  }

  /**
   * 同步数据到本对象
   * @private
   */
  _updateProperty() {
    const ee = this._bo.extensionElements || {};
    const values = ee.values || [];
    for (const value of values) {
      if (value.$type === 'camunda:Properties') {
        for (const prop of value.values) {
          if (prop.$type === 'camunda:Property') {
            this._props[prop.name] = prop.value;
          }
        }
      }
    }
  }

  /**
   * 同步表达式到本对象
   * @private
   */
  updateExpression() {
    const exp = this._bo.conditionExpression || {};
    this._exp = exp.body;
  }

  /**
   * 把props 写到 element上面去
   */
  writeProps2Ele(check = true) {
    if (this.type !== NodeType.EXAMINE_TASK && check) {
      throw new Error('非审批节点没有数据');
    }
    if (!this._bo.extensionElements) {

    }

  }

  getExtensionElements(){
    return this._bo.get('extensionElements');
  }

  _createExtensionElements(element, bpmnFactory) {

    // let command = []
    let parent = elementHelper.createElement('bpmn:ExtensionElements', {values: []}, element.businessObject, bpmnFactory);
    // 创建bpmn:ExtensionElements节点
    let cmd = cmdHelper.updateBusinessObject(element, element.businessObject, {extensionElements: parent});
    // 更新element.bo节点
    // command.push(cmd)

    // 创建camunda:Properties节点
    const properties = elementHelper.createElement('camunda:Properties', {}, parent, bpmnFactory);

    // 添加ExtensionElements节点
    // command.push(
    cmdHelper.addAndRemoveElementsFromList(element, parent, 'values', 'extensionElements', [properties], []);
    //)

    // 确定property节点的属性
    // let propertyProps = {};
    // propertyProps.name = currentName;
    // propertyProps.value = setValue.value;

    // 创建property节点
    // let property = elementHelper.createElement('camunda:Property', propertyProps, properties, bpmnFactory)
    // 将property节点添加进入刚才创建的properties中
    // command.push(
    // cmdHelper.addElementsTolist(element, properties, 'values', [property])
    // );

    // console.log(command)
    // return command

  }

  /**
   * 确保有 extensionElements
   */
  confirmExtensionElements(element,bpmnFactory){

    // console.log(bpmnFactory);

    // console.log();

    const cmd = [];

    // debugger
    let els = this.getExtensionElements();
    if(els){
      return;
    }
    bpmnFactory.create('bpmn:ExtensionElements');
  // const els = elementHelper.createElement('bpmn:ExtensionElements', {values: []}, element.businessObject, bpmnFactory);

  // console.log(els);

    cmd.push(cmdHelper.updateBusinessObject(element, element.businessObject, {extensionElements: els}));

    // console.log(element);
    // const properties = elementHelper.createElement('camunda:Properties', {}, els, bpmnFactory);
    // cmdHelper.addAndRemoveElementsFromList(element, els, 'values', 'extensionElements', [properties], []);

    // console.log(cmd);
    return cmd;
  }

  /**
   * 每一次被选中都要调用
   */
  onActive(element,bpmnFactory){

    this.updateType();

    // return this.confirmExtensionElements(element,bpmnFactory);

    // console.log(this.getExtensionElements())



  }

}


/**
 * 从挂载的节点上获取 TaskNode
 * @param element
 */
const getNode = element => {
  const bo = getBusinessObject(element);
  return bo['__task_node'] = bo['__task_node'] || new TaskNode(bo);
};


export {
  is,
  getBusinessObject,
  TaskNode,
  NodeType,
  getNode
}